#!/bin/sh
#------------------------------------------------------------------------------
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2018-2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# Script
#     makeAdios2
#
# Description
#     Build script for ADIOS2
#
# ----------------------------------------------
# NO USER-CONFIGURABLE SETTINGS WITHIN THIS FILE
#------------------------------------------------------------------------------
# Dynamic library ending (default is .so)
[ "$(uname -s)" = Darwin ] && EXT_SO=.dylib || EXT_SO=.so

# Short-circuit test for an installation
if [ "$1" = "-test" ]
then
    [ "$#" -eq 2 ] || { echo "${0##*/} -test : needs 1 argument"; exit 1; }
    dir="${2%/}" # <- *_ARCH_PATH
    [ -d "$dir/include" ] || exit 2

    package="adios2"
    libName="libadios2"
    for lib in \
        "$dir/lib/$libName$EXT_SO" \
        "$dir/lib$WM_COMPILER_LIB_ARCH/$libName$EXT_SO" \
        ;
    do
        if [ -r "$lib" ]
        then
            echo "    $package include: $dir/include"
            echo "    $package library: ${lib%/*}"
            exit 0
        fi
    done
    exit 2
fi
#------------------------------------------------------------------------------
# Run from third-party directory only
cd "${0%/*}" || exit
wmakeCheckPwd "$WM_THIRD_PARTY_DIR" 2>/dev/null || {
    echo "Error (${0##*/}) : not located in \$WM_THIRD_PARTY_DIR"
    echo "    Check your OpenFOAM environment and installation"
    exit 1
}
. etc/tools/ThirdPartyFunctions
#------------------------------------------------------------------------------
# ADIOS2 version from OpenFOAM etc/config.sh file:
_foamConfig adios2

adiosPACKAGE=${adios2_version:-adios-none}

#------------------------------------------------------------------------------
usage() {
    exec 1>&2
    while [ "$#" -ge 1 ]; do echo "$1"; shift; done
    cat<<USAGE

usage: ${0##*/} [OPTION] [adios-VERSION]
options:
  -force        Force compilation, even if include/library already exists
  -gcc          Force use of gcc/g++
  -cmake PATH   With cmake from the given path
  -help

* Build ADIOS2
      $adiosPACKAGE

USAGE
    showDownloadHint ADIOS2
    exit 1
}
#------------------------------------------------------------------------------
exportCompiler minimal  # Minimal compiler info for CMake/configure

unset optForce

# Parse options
while [ "$#" -gt 0 ]
do
    case "$1" in
    '') ;;      # Ignore empty
    -h | -help) usage   ;;
    -gcc)       useGcc  ;;
    -force)     optForce=true ;;

    -cmake)
        [ "$#" -ge 2 ] || die "'$1' option requires an argument"
        CMAKE_PATH="${2%%/}"
        shift
        ;;
    ADIOS2-[0-9]* | ADIOS2-git* | ADIOS-[0-9]* | ADIOS-git* | \
    adios2-[0-9]* | adios2-git* | adios-[0-9]* | adios-git*)
        adiosPACKAGE="${1%%/}"
        ;;
    *)
        die "unknown option/argument: '$1'"
        ;;
    esac
    shift
done

[ -n "$adiosPACKAGE" ] || die "The adios2-VERSION was not specified"

# nothing to build
if _foamIsNone "$adiosPACKAGE"
then
    echo "Using adios-none (skip ThirdParty build of ADIOS)"
    exit 0
elif _foamIsSystem "$adiosPACKAGE"
then
    echo "Using adios-system"
    exit 0
fi

# Known build issues for mingw (various things)
case "$WM_COMPILER" in
(Mingw*)
    if [ "$optForce" = true ]
    then
        echo "Warning: adios2 - known compilation issues with $WM_COMPILER"
    else
        echo "Skipping adios2 - known compilation issues with $WM_COMPILER"
        exit 0
    fi
    ;;
esac

#------------------------------------------------------------------------------
#
# Build ADIOS
#   ADIOS2_SOURCE_DIR : location of the original sources
#   ADIOS2_ARCH_PATH  : installation directory

ADIOS2_SOURCE_DIR=$sourceBASE/$adiosPACKAGE
ADIOS2_ARCH_PATH=$installBASE/$adiosPACKAGE

: ${FOAM_MPI:=dummy}

echo
echo ========================================
echo "Build adios library $adiosPACKAGE for $FOAM_MPI"
echo

# Needs future adjustment
# - for mpi-specific library locations
if [ -z "$optForce" ] \
&& [ -f "$ADIOS2_ARCH_PATH/include/adios2.h" ] \
&& {
    [ -r "$ADIOS2_ARCH_PATH/lib/libadios2$EXT_SO" ] \
 || [ -r "$ADIOS2_ARCH_PATH/lib$WM_COMPILER_LIB_ARCH/libadios2$EXT_SO" ]
}
then
    echo "    ADIOS2 already built : $ADIOS2_ARCH_PATH"
else
    # CMake options often lag the configure ones
    echo "Starting build: $adiosPACKAGE (using cmake)"
    echo
    (
        buildDIR=$buildBASE/$adiosPACKAGE
        cd "$ADIOS2_SOURCE_DIR" || exit
        export GIT_DIR="$PWD/.git" # Mask seeing our own git-repo

        applyPatch $adiosPACKAGE $ADIOS2_SOURCE_DIR

        # Remove any existing build folder and recreate
        rm -rf $ADIOS2_ARCH_DIR
        rm -rf $buildDIR 2>/dev/null
        mkdir -p $buildDIR

        # May not work properly with FOAM_MPI = dummy
        if [ "$FOAM_MPI" != dummy ]
        then
            CC=mpicc
            CXX=mpicxx
        fi

        cmake=$(findCMake)

        # Install into lib64/
        cd $buildDIR && $cmake \
            -DCMAKE_INSTALL_PREFIX=$ADIOS2_ARCH_PATH \
            -DCMAKE_BUILD_TYPE=Release \
            -DADIOS2_USE_Fortran=FALSE \
            -DADIOS2_BUILD_EXAMPLES=FALSE \
            ${WM_QUIET:+-DCMAKE_RULE_MESSAGES=OFF} \
            $ADIOS2_SOURCE_DIR \
        && make -j $WM_NCOMPPROCS all \
        && make install \
        && echo "Built: $adiosPACKAGE"
    ) || {
        echo "Error building: $adiosPACKAGE"
        exit 1
    }
fi

# -----------------------------------------------------------------------------
